size <- 12. Later this will be the number of rows of the matrix.x <- rnorm( size ).x1 by adding (on average 10 times smaller) noise to x: x1 <- x + rnorm( size )/10.x and x1 should be close to 1.0: check this with function cor.x2 and x3 by adding (other) noise to x.size <- 12
x <- rnorm( size )
x1 <- x + rnorm( size )/10
cor( x, x1 )
[1] 0.9992308
x2 <- x + rnorm( size )/10
x3 <- x + rnorm( size )/10
x1, x2 and x3 column-wise into a matrix using m <- cbind( x1, x2, x3 ).m.m.heatmap( m, Colv = NA, Rowv = NA, scale = "none" ).m <- cbind( x1, x2, x3 )
class( m )
[1] "matrix"
head( m )
x1 x2 x3
[1,] 1.1601330 1.0423387 1.1795919
[2,] 2.1009798 2.0998961 2.1039205
[3,] 1.7784955 1.6478967 1.6440722
[4,] -0.2528933 -0.1649807 -0.3456783
[5,] -1.6158324 -1.6269961 -1.5456896
[6,] 0.9372104 0.8230043 0.8056392
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
# x1, x2, x3 follow similar color pattern, they should be correlated
y1…y4 (but not correlated with x), of the same length size.m from columns x1…x3,y1…y4 in some random order.y <- rnorm( size )
y1 <- y + rnorm( size )/10
y2 <- y + rnorm( size )/10
y3 <- y + rnorm( size )/10
y4 <- y + rnorm( size )/10
m <- cbind( y4, y3, x2, y1, x1, x3, y2 )
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
cc <- cor( m ) to build the matrix of correlation coefficients between columns of m.round( cc, 3 ) to show this matrix with 3 digits precision.cc <- cor( m )
round( cc, 3 ) #
y4 y3 x2 y1 x1 x3 y2
y4 1.000 0.990 -0.425 0.990 -0.404 -0.396 0.996
y3 0.990 1.000 -0.356 0.996 -0.333 -0.324 0.993
x2 -0.425 -0.356 1.000 -0.378 0.998 0.997 -0.413
y1 0.990 0.996 -0.378 1.000 -0.360 -0.352 0.995
x1 -0.404 -0.333 0.998 -0.360 1.000 0.998 -0.396
x3 -0.396 -0.324 0.997 -0.352 0.998 1.000 -0.385
y2 0.996 0.993 -0.413 0.995 -0.396 -0.385 1.000
heatmap( cc, symm = TRUE, scale = "none" )
# E.g. value for (row: x1, col: y1) is the corerlation of vectors x1, y1.
# Values of 1.0 are on the diagonal: e.g. x1 is perfectly correlated with x1.
# Correlations between x, x vectors are close to 1.0.
# Correlations between y, y vectors are close to 1.0.
# Correlations between x, y vectors are close to 0.0.
Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC